package com.virtualCat.controller.portal;

import com.virtualCat.common.Msg;
import com.virtualCat.vo.UserInfo;
import com.virtualCat.service.FeedService;
import com.virtualCat.service.UserService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/feed")
@Api(value = "feed",description="喂养猫咪的接口")
public class FeedController {
    @Autowired
    private FeedService feedService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="1.登陆授权",httpMethod="GET",notes="授权")
    @ResponseBody
    public Msg login(@RequestParam("code")@ApiParam(name="code",value="code的值")String code){
//        String trim_code = code.trim();
        return feedService.login(code);
    }

    @RequestMapping(value = "/user_info",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="2.添加用户信息",httpMethod="POST",notes="录入微信用户信息")
    @ResponseBody
    public Msg userInfo(@RequestBody @ApiParam(name="userInfo",value="用户")UserInfo userInfo) throws Exception{
        return feedService.createUser(userInfo);
    }


    @RequestMapping(value = "/raise",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="3.首页",httpMethod="GET",notes="登陆首个界面")
    @ResponseBody
    public Msg raiseCat(@RequestParam("openid")String openid){
        return feedService.raiseCat(openid);
    }

    @RequestMapping(value = "/feed_cat",
                    method = RequestMethod.POST,
                    produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="4.喂养猫咪",httpMethod="POST",notes="喂养猫咪")
    @ResponseBody
    public Msg feedCat(@RequestParam("openid")String openid) throws Exception{
        return feedService.feedCat(openid);
    }

    @RequestMapping(value = "/check_cat",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="11.是否拥有猫咪",httpMethod="POST",notes="是否拥有猫咪")
    @ResponseBody
    public Msg checkCat(@RequestParam("openid")String openid){
        return feedService.checkCat(openid);
    }

    @RequestMapping(value = "/cat_list",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="6.猫咪列表",httpMethod="GET",notes="猫咪列表")
    @ResponseBody
    public Msg catInfo(){
        return feedService.catList();
    }

    @RequestMapping(value = "/find_food",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="5.寻找猫粮",httpMethod="POST",notes="寻找猫粮")
    @ResponseBody
    public  Msg findFood(@RequestParam("openid")String openid){
        return feedService.discoverFood(openid);
    }

    @RequestMapping(value = "/exchange",method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="7.兑换",httpMethod="POST",notes="兑换")
    @ResponseBody
    public Msg exchange(@RequestParam("openid") String openid){
        return feedService.exchange(openid);
    }

    @RequestMapping(value = "/throw",method = RequestMethod.DELETE,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="8.抛弃猫咪",httpMethod="DELETE",notes="抛弃猫咪")
    @ResponseBody
    public Msg throwCat(@RequestParam("openid") String openid) throws Exception{
        return feedService.throwCat(openid);
    }

    @RequestMapping(value = "/address",
                    method = RequestMethod.POST,
                    produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="9.获取地址",httpMethod="POST",notes="获取地址")
    @ResponseBody
    public Msg gainAddress(@RequestParam("openid")String openid) throws Exception{
        return feedService.gainAddress(openid);
    }

    @RequestMapping(value = "/update_address",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="10.修改地址",httpMethod="POST",notes="修改地址")
    @ResponseBody
    public Msg updateAddress(@RequestParam("openid")@ApiParam(name="openid",value="用户的openid")String openid,
                             @RequestParam("phone")@ApiParam(name="phone",value="电话")String phone,
                             @RequestParam("receiver")@ApiParam(name="receiver",value="收件人")String receiver,
                             @RequestParam("area")@ApiParam(name="area",value="地区街道")String area,
                             @RequestParam("build_number")@ApiParam(name="build_number",value="门牌号")String build_number){
        return userService.updateAddress(openid,receiver,phone,area,build_number);
    }

    @RequestMapping(value = "/share",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="14.分享首页",httpMethod="POST",notes="进入分享首页")
    @ResponseBody
    public Msg share(@RequestParam("user_openid")@ApiParam(name="user_openid",value="被喂养者的openid")String user_openid,
                     @RequestParam("share_openid")@ApiParam(name="share_openid",value="喂养者的openid")String share_openid){
        return feedService.share(user_openid,share_openid);
    }

    @RequestMapping(value = "/share_raise",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="15.分享喂养",httpMethod="POST",notes="喂养")
    @ResponseBody
    public Msg shareRaise(@RequestParam("user_openid")@ApiParam(name="user_openid",value="被喂养者的openid")String user_openid,
                     @RequestParam("share_openid")@ApiParam(name="share_openid",value="喂养者的openid")String share_openid,
                         @RequestParam("date")@ApiParam(name="date",value="卡片的有限日期")Date date){
        return feedService.shareRaise(user_openid,share_openid,date);
    }

}
